﻿/*-----------------------------------------------
// File: UltimateEndState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class UltimateEndState : Fsm.FsmStateBase<BattleRoundState>
    {
        protected BattleRoundCtrl battleRoundCtrl {
            get { return this.controller as BattleRoundCtrl; }
        }

        public UltimateEndState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl) {
        }

        float cameraEndTime
        {
            get { return Dict.Blo.DictConfigBlo.GetUltimateEndCameraTime(); }
        }
        float showWordTipTime {
            get { return 0.3f; }
        }

        UltimateSkill currentSkill;


        bool isShowWordTip = false;

        float deltaTime = 0f;
        public override void Enter()
        {
            deltaTime = 0f;
            currentSkill = battleRoundCtrl.GetSource().GetSkill(SkillType.Ultimate) as UltimateSkill;
            isShowWordTip = false;
            Debug.Log("ultimate end time: " + Time.time);
            CameraController.Instance.UltimateEnd(currentSkill.cameraEndType);
        }

        void ShowUltimateHpWordTip() {
            Dictionary<string, double> hpDeltas = new Dictionary<string, double>();
            foreach (var package in battleRoundCtrl.GetUltimateCachePackages())
            {
                foreach (var hpExport in package.hpExports)
                {
                    if (hpDeltas.ContainsKey(hpExport.targetUid))
                        hpDeltas[hpExport.targetUid] += hpExport.infValue;
                    else
                        hpDeltas[hpExport.targetUid] = hpExport.infValue;
                }
            }
            foreach (var kvp in hpDeltas)
            {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(kvp.Key);
                if (kvp.Value > 0)
                {
                    BattleViewCtrl.GetInstance().ShowWordTip(bc, (int)kvp.Value, WordType.ADD);
                }
                else
                {
                    BattleViewCtrl.GetInstance().ShowWordTip(bc, (int)kvp.Value, WordType.DAMAGE);
                    bc.PlayHit(0.5f);
                }
                if (bc.camp == currentSkill.GetOwnBC().camp)
                {
                    if (currentSkill.ultiFInalBeHitOwnParticleData != null)
                    {
                        bc.PlayParticle(currentSkill.ultiFInalBeHitOwnParticleData, true);
                    }
                }
                else
                {
                    if (currentSkill.ultiFinalBeHitParticleData != null)
                    {
                        bc.PlayParticle(currentSkill.ultiFinalBeHitParticleData, true);
                    }

                }
                // bc.ShowHpBar(bc.GetAniLength(AniName.BEHIT));
            }
            isShowWordTip = true;
            Debug.Log("shake time: " + Time.time);
            if (currentSkill.cameraEndType == UltimateSkill.UltimateCameraEndType.FocusEnemy) {
                CameraController.Instance.Shake(2);
            }

            List<string> killActors = new List<string>();
            foreach (var package in battleRoundCtrl.GetUltimateCachePackages())
            {
                foreach (var uid in package.destroyObjs.actorUids)
                {
                    killActors.Add(uid);
                }
            }
            foreach (var uid in killActors)
            {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(uid);
                BattleViewCtrl.GetInstance().StartCoroutine(ShowDead(bc, bc.GetAniLength(AniName.BEHIT)));
            }

            foreach (var package in battleRoundCtrl.GetUltimateCachePackages())
            {
                package.DoRebornExport();
            }

            AudioManager.Instance.PlaySingle(currentSkill.ultimateEndHitAudio);
        }

        IEnumerator ShowDead(BattleCharacter bc, float duration) {
            yield return new WaitForSeconds(duration);
            bc.SwitchState(ActorState.Dead);
        }


        public override void Update()
        {
            deltaTime += Time.deltaTime;
            if (deltaTime >= showWordTipTime && !isShowWordTip) {
                ShowUltimateHpWordTip();
            }
            if (deltaTime >= cameraEndTime) {
                if (IsRoundWaitState())
                {
                    battleRoundCtrl.SwitchState(BattleRoundState.RoundWaitState);
                }
                else {
                    battleRoundCtrl.SwitchState(BattleRoundState.AttackingState);
                }
            }
        }

        public override void Leave()
        {
            BattleManager.Instance.battlePanel.UltimateOver();
            BattleViewCtrl.GetInstance().isUltimateBeginCast = false;
        }

        bool IsRoundWaitState()
        {
            foreach (var uid in BattleViewCtrl.GetInstance().GetActorUids(battleRoundCtrl.currentCamp))
            {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(uid);
                if (bc.GetCurrrentStateType() != ActorState.AttackReady)
                {
                    return false;
                }
            }
            return true;
        }
    }

}